package com.ztgf.shopping.api.common.interceptor;

import com.ztgf.sys.models.service.ISysLogService;
import com.ztgf.user.models.dto.UserInfoDTO;
import com.ztgf.user.models.service.IUserInfoService;
import com.ztgf.user.models.service.IUserLoginService;
import com.ztgf.user.models.service.IUserRoleRelationService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Aspect
@Component
public class ShoppingAspect {

	@DubboReference
	private ISysLogService sysLogService;

	@DubboReference
	private IUserRoleRelationService userRoleRelationService;

	@DubboReference
    private IUserLoginService userLoginService;

	@DubboReference
    private IUserInfoService userInfoService;

	@Pointcut("@annotation(com.ztgf.shopping.api.common.ShoppingAspect)")
	public void detection() {
	}

	@Before("detection()")
	public void before(JoinPoint joinPoint) {
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
		HttpSession session = request.getSession();
		//获取session的Id
		String sessionId = session.getId();
		//判断是不是主账号（主账号拥有所有权限）
        UserInfoDTO userInfoDTO = userLoginService.getUserBySessionId(sessionId);
        if(userInfoDTO.getType() == 0){//主账号

		}
        //获取主账号的信息

		//判断子账号拥有的角色是否有这个权限
		//SysLogSaveBO sysLog = new SysLogSaveBO();
		// 获取动作Action释义
//		sysLog.setActionName(getMethodSysLogsAnnotationValue(joinPoint));
//		// 获取IP
//		sysLog.setIp(RequestUtil.getClientIp(request));
//		sysLog.setAjax(RequestUtil.ajax(request) ? 1 : 0);
//		sysLog.setUri(request.getRequestURI());
//		String s = this.paramFilter(joinPoint.getArgs());
//		// 根据系统需求自定义
//		sysLog.setParams(s.length() > 500 ? "数据过大，不给予记录" : s);
//		sysLog.setHttpMethod(request.getMethod());
//		sysLog.setClassMethod(
//		joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName() + "()");
//		// 判断身份是否为空
//		HttpSession session = request.getSession();
//		//获取session的Id
//		String sessionId = session.getId();
//		SysUserDTO sysUserDTO = sysUserService.getUserBySessionId(sessionId);
//		sysLog.setUsername(sysUserDTO.getAccount());
//		sysLog.setUid(sysUserDTO.getId()+"");
//		sysLog.setCreateDate(new Date());
//		sysLogService.add(sysLog);
	}

}
